﻿using DataAccess.Interfaces;
using DataAccess.Mappings;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace DataAccess.Services
{
    public class NHibernateSessionProvider : INHibernateSessionProvider
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                {//c => c.FromAppSetting("SocialityMainDBConnection"))
                    _sessionFactory = Fluently.Configure()
                        .Database(
                            MsSqlConfiguration.MsSql2008.ConnectionString("Data Source=EPBYMINW0973\\SQLEXPRESS;Database=uscData;Integrated Security=True;"))
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>())
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserPreferenceMap>())
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PreferenceMap>())
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SecurityKeyMap>())
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionMap>())
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SocialNetworkMap>())
                         .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserAccountMap>())
                                .BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }

        ISessionFactory INHibernateSessionProvider.SessionFactory
        {
            get { return SessionFactory; }
        }

        public ISession OpenNHibernateSession()
        {
           return SessionFactory.OpenSession();
        }

    }
}
